package com.mycar2w.cloud.service.serviceimpl;

import com.mycar2w.cloud.entity.ApplieOrder;
import com.mycar2w.cloud.entity.Car;
import com.mycar2w.cloud.entity.CarBasicParameter;
import com.mycar2w.cloud.entity.CarChassisBrakeParameter;
import com.mycar2w.cloud.entity.CarEngineParameter;
import com.mycar2w.cloud.entity.CarInnerConfigutation;
import com.mycar2w.cloud.entity.CarOutConfiguration;
import com.mycar2w.cloud.entity.CarSecurityConfiguration;
import com.mycar2w.cloud.entity.CarSystemTestBrakeEnginecompartment;
import com.mycar2w.cloud.entity.CarSystemTestChassis;
import com.mycar2w.cloud.entity.CarSystemTestIndicatorLight;
import com.mycar2w.cloud.entity.CarTestAccelerate;
import com.mycar2w.cloud.entity.CarTestDecelerationBraking;
import com.mycar2w.cloud.entity.CarTestStart;
import com.mycar2w.cloud.entity.CarTestStartup;
import com.mycar2w.cloud.entity.CarTestUniformDriving;
import com.mycar2w.cloud.entity.TTransaction;
import com.mycar2w.cloud.entity.TUser;
import com.mycar2w.cloud.service.repository.ApplieOrderRepository;
import com.mycar2w.cloud.service.repository.CarBasicParameterR;
import com.mycar2w.cloud.service.repository.CarChassisBrakeParameterR;
import com.mycar2w.cloud.service.repository.CarEngineParameterR;
import com.mycar2w.cloud.service.repository.CarInnerConfigutationR;
import com.mycar2w.cloud.service.repository.CarOutConfigurationR;
import com.mycar2w.cloud.service.repository.CarRepository;
import com.mycar2w.cloud.service.repository.CarSecurityConfigurationR;
import com.mycar2w.cloud.service.repository.CarSystemTestBrakeEnginecompartmentR;
import com.mycar2w.cloud.service.repository.CarSystemTestChassisR;
import com.mycar2w.cloud.service.repository.CarSystemTestIndicatorLightR;
import com.mycar2w.cloud.service.repository.CarTestAccelerateR;
import com.mycar2w.cloud.service.repository.CarTestDecelerationBrakingR;
import com.mycar2w.cloud.service.repository.CarTestStartR;
import com.mycar2w.cloud.service.repository.CarTestStartupR;
import com.mycar2w.cloud.service.repository.CarTestUniformDrivingR;
import com.mycar2w.cloud.service.repository.TTransactionRepository;
import com.mycar2w.cloud.service.repository.TUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * created time 2017/4/4
 *
 * @author ljc
 * @version V1.0
 */
@Service
public class CarOptionService {
    @Autowired
    private ApplieOrderRepository applieOrderRepository;
    @Autowired
    private TUserRepository tUserRepostirory;
    @Autowired
    private CarRepository carRepository;
    @Autowired
    private CarBasicParameterR carBasicParameterR;
    @Autowired
    private CarChassisBrakeParameterR carChassisBrakeParameter;
    @Autowired
    private CarEngineParameterR carEngineParameterR;
    @Autowired
    private CarInnerConfigutationR carInnerConfigutationR;
    @Autowired
    private CarSecurityConfigurationR carSecurityConfigurationR;
    @Autowired
    private CarSystemTestBrakeEnginecompartmentR carSystemTestBrakeEnginecompartmentR;
    @Autowired
    private CarSystemTestChassisR carSystemTestChassisR;
    @Autowired
    private CarSystemTestIndicatorLightR carSystemTestIndicatorLightR;
    @Autowired
    private CarTestAccelerateR carTestAccelerateR;
    @Autowired
    private CarTestDecelerationBrakingR carTestDecelerationBrakingR;
    @Autowired
    private CarTestStartR carTestStartR;
    @Autowired
    private CarTestStartupR carTestStartupR;
    @Autowired
    private CarTestUniformDrivingR carTestUniformDrivingR;
    @Autowired
    private CarOutConfigurationR carOutConfigurationR;
    @Autowired
    private TTransactionRepository tTransactionRepository;


    public void saveApplieOrder(ApplieOrder applieOrder){
        applieOrder.setId(new Timestamp(System.currentTimeMillis()).getTime());
        applieOrder.setCreateTime(new Timestamp(System.currentTimeMillis()));
        applieOrderRepository.save(applieOrder);
    }

    public List<ApplieOrder> findByPhone(String phone) {
        TUser tUser= tUserRepostirory.findByPhone(phone);
        Long userId=tUser.getId();
        List<ApplieOrder> appliedOrderList=applieOrderRepository.findByUserId(userId);
        return appliedOrderList;
    }

    public ApplieOrder findByApplieOrderId(Long applieOrderId) {
        ApplieOrder applieOrder=applieOrderRepository.findById(applieOrderId);
        return applieOrder;
    }

    public List<Car> findAllSaleCarByPhone(String phone) {
        TUser tUser=tUserRepostirory.findByPhone(phone);
        List<Car> cars=carRepository.findByUserId(tUser.getId());
        return cars;
    }

    public Car findByCarId(Long carId) {
        Car car=carRepository.findById(carId);
        return car;
    }

    public TUser getUserByCarId(Long carId) {
        return tUserRepostirory.findById(carRepository.findById(carId).getUserId());
    }

    public CarBasicParameter getCarBasicParameter(Long carId) {
        return carBasicParameterR.findByCarId(carId);
    }

    public CarChassisBrakeParameter CarChassisBrakeParameter(Long carId) {
        return carChassisBrakeParameter.findByCarId(carId);
    }

    public CarEngineParameter getCarEngineParameter(Long carId) {
        return carEngineParameterR.findByCarId(carId);
    }

    public CarInnerConfigutation getCarInnerConfigutation(Long carId) {
        return carInnerConfigutationR.findByCarId(carId);
    }

    public CarOutConfiguration getCarOutConfigutation(Long carId) {
        return carOutConfigurationR.findByCarId(carId);
    }

    public CarSecurityConfiguration getCarSecurityConfiguration(Long carId) {
        return carSecurityConfigurationR.findByCarId(carId);
    }

    public CarSystemTestBrakeEnginecompartment getCarSystemTestBrakeEnginecompartment(Long carId) {
        return carSystemTestBrakeEnginecompartmentR.findByCarId(carId);
    }

    public CarSystemTestChassis getCarSystemTestChassis(Long carId) {
        return carSystemTestChassisR.findByCarId(carId);
    }

    public CarSystemTestIndicatorLight getCarSystemTestIndicatorLight(Long carId) {
        return carSystemTestIndicatorLightR.findByCarId(carId);
    }

    public CarTestAccelerate getCarTestAccelerate(Long carId) {
        return carTestAccelerateR.findByCarId(carId);
    }

    public CarTestDecelerationBraking getCarTestDecelerationBraking(Long carId) {
        return carTestDecelerationBrakingR.findByCarId(carId);
    }

    public CarTestStart getCarTestStart(Long carId) {
        return carTestStartR.findByCarId(carId);
    }

    public CarTestStartup getCarTestStartup(Long carId) {
        return carTestStartupR.findByCarId(carId);
    }

    public CarTestUniformDriving getCarTestUniformDriving(Long carId) {
        return carTestUniformDrivingR.findByCarId(carId);
    }

    public List<TTransaction> transactionListGet(String phone, Integer pageIndex) {
        Long userId=tUserRepostirory.findByPhone(phone).getId();
        Page<TTransaction> tTransactions=tTransactionRepository.findTransactions(userId,pageIndex);
        return tTransactions.getContent();
    }

    public TTransaction findTransactionInfo(Long transactionId) {
        TTransaction tTransaction=tTransactionRepository.findById(transactionId);
        return tTransaction;
    }
}
